﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Helpers
{
	using System;
	using System.IO;
	using System.Xml.Serialization;
	using Falcon.Core.Extensions;

	public class XmlHelper<T> : IDisposable where T : class
	{
		private readonly Stream _stream;

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlHelper&lt;T&gt;"/> class.
		/// </summary>
		public XmlHelper()
		{
			_stream = null;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlHelper&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="path">The path.</param>
		public XmlHelper(string path)
			: this(path, FileAccess.Read)
		{

		}

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlHelper&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="path">The path.</param>
		/// <param name="access">The access.</param>
		public XmlHelper(string path, FileAccess access)
		{
			_stream = new FileStream(path, FileMode.Open, access);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlHelper&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="stream">The stream.</param>
		public XmlHelper(Stream stream)
		{
			_stream = stream;
		}

		/// <summary>
		/// Loads this instance.
		/// </summary>
		/// <returns></returns>
		public T Load()
		{
			using (TextReader reader = new StreamReader(_stream))
			{
				var serializer = new XmlSerializer(typeof(T));
				var value = serializer.Deserialize(reader).As<T>();

				reader.Close();

				return value;
			}
		}

		/// <summary>
		/// Saves the specified value.
		/// </summary>
		/// <param name="value">The value.</param>
		public void Save(T value)
		{
			using (TextWriter writer = new StreamWriter(_stream))
			{
				var serializer = new XmlSerializer(typeof(T));

				serializer.Serialize(writer, value);

				writer.Close();
			}
		}

		/// <summary>
		/// Releases unmanaged and - optionally - managed resources
		/// </summary>
		/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				_stream.Close();
			}
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}
	}
}